package com.oceansoft.core.jxkh.controller;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.jxkh.domain.Khlx;
import com.oceansoft.core.jxkh.service.IKhlxService;
import com.oceansoft.core.jxkh.service.IKhyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/12.
 */

@Controller
@RequestMapping("/khlx")
public class KhlxController {

    @Resource
    private IKhlxService khlxService;

    @Resource
    private IKhyService khyService;

    @RequestMapping(value="allKhlxInfo.json")
    public void allKhlxInfo(HttpServletRequest request, ModelMap modelMap){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);

        Pagination khlxPag= khlxService.findAllKhlx(paramap);
        modelMap.addAttribute("rows",khlxPag.getList());
        modelMap.addAttribute("total",khlxPag.getTotalRows());
    }

    @RequestMapping(value="editKhlxInfo",method = RequestMethod.POST)
    public void editKhlxInfo(HttpServletRequest request, ModelMap modelMap,Khlx khlx){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khlxService.editKhlxInfo(khlx);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="findKhlxInfo.html")
    public String findKhlxInfo(int dbid,ModelMap modelMap){
        Khlx khlx = khlxService.findKhlxInfoByDbid(dbid);
        List list = khyService.findAllKhyName();
        modelMap.addAttribute("khyNameList",list);
        modelMap.addAttribute("khlx",khlx);
        return "jump/khlxsz_bj";
    }

    @RequestMapping(value="deleteKhlxInfo.json")
    public void deleteKhlxInfo(int dbid, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khlxService.deleteKhlxInfoByDbid(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="addKhlxInfo.json",method =RequestMethod.POST)
    public void addKhlxInfo(Khlx khlx, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khlxService.addKhlxInfo(khlx);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="batchDeleteKhlxInfo.json")
    public void batchDeleteKhlxInfo(int[] dbids,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khlxService.batchDeleteKhlxInfo(dbids);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    /*
    * 考核类别设置
   * */
    @RequestMapping(value="khlxsz.html")
    public String khlxsz(HttpServletRequest request, ModelMap modelMap) {
        List list = khyService.findAllKhyName();
        modelMap.addAttribute("khyNameList",list);
        return "jump/khlxsz";
    }

    @RequestMapping(value="findKhlxNameByItemaId.json")
    public void findKhlxNameByItemaId(HttpServletRequest request, ModelMap modelMap,int itemaId) {
        List list = khlxService.findKhlxNameByItemaId(itemaId);
        modelMap.addAttribute("khlxNameList",list);
    }
}
